package com.sfdk.admin.helpers
{
	import com.sfdk.admin.ApplicationData;
	import com.sfdk.admin.data.api.IDataSource;
    import com.sfdk.admin.data.resourceEditor.ResourceCategory;
    import com.sfdk.admin.data.resourceEditor.ResourceElement;
    import com.sfdk.admin.data.resourceEditor.ResourceSubCategory;
    import com.sfdk.data.api.ITreeDataProvider;
import com.sfdk.events.IOEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
    import flash.utils.Dictionary;

    import mx.collections.ArrayCollection;
	import mx.collections.IList;
	
	/**
	 * Utility class used to read data from directory on the server
	 */
	public class ResourceFileReader extends FileReader
	{
        public function ResourceFileReader(moduleName : String, fileType : String, dataClass : Class, fileFilter : String, classParameter : * = null, classParameter2 : * = null)
		{
			super(moduleName, fileType, dataClass, fileFilter, classParameter, classParameter2);
		}

		override protected function onDirectoryListed(event : Event):void
		{
			var fileListString : String = event.target.data;
			var data : ArrayCollection = new ArrayCollection(); // Of Resource Element
            if(fileListString != "")
			{
				var fileList : Array = fileListString.split(":");
				var currentFile : ResourceElement;
				_toLoad = fileList.length;
				
				for (var i:int = 0; i < fileList.length; i++)
				{
					if(fileList[i] != "")
					{
                        var ext: String = fileList[i].substr(fileList[i].length - 3, 3);
                        if(ext != "php")
                        {
                            var fileListSplit : Array = fileList[i].split("/");
                            var categoryName : String = fileListSplit[1];
                            var currentCategory : ResourceCategory = null;
                            for each(var category : ResourceCategory in data)
                            {
                                if(category.categoryName == categoryName)
                                {
                                    currentCategory = category;
                                    break;
                                }
                            }
                            if(!currentCategory)
                            {
                                currentCategory = new ResourceCategory();
                                currentCategory.categoryName = categoryName;
                                data.addItem(currentCategory);
                            }

                            var subCategoryName : String = fileListSplit[2];
                            var currentSubCategory : ResourceSubCategory = null;
                            for each(var subCategory : ResourceSubCategory in currentCategory.subCategories)
                            {
                                if(subCategory.categoryName == subCategoryName)
                                {
                                    currentSubCategory = subCategory;
                                    break;
                                }
                            }
                            if(!currentSubCategory)
                            {
                                currentSubCategory = new ResourceSubCategory();
                                currentSubCategory.categoryName = subCategoryName;
                                currentCategory.subCategories.addItem(currentSubCategory);
                            }

                            currentFile = new ResourceElement(ApplicationData.BASE_LOCATION+_moduleName+_fileType+"/", fileList[i]);
                            //currentFile.addEventListener(IOEvent.LOAD_COMPLETE, onFileLoaded, false, 0, true);

                            currentSubCategory.elements.addItem(currentFile);
                        }
                        else
                        {
                            _toLoad--;
                        }

					}
					else
						_toLoad--;
				}
				_data = data;
			}
			_data = data;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function onFileLoaded(e : Event) : void
		{
			_toLoad--;
			if(_toLoad == 0)
			{
				dispatchEvent(new Event(Event.COMPLETE));
				_isLoaded = true;
			}
		}
	}
	
	
}